package com.woniu.controller;

import com.woniu.pojo.Trainer;
import com.woniu.entity.dto.TrainerSearchDTO;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.vo.TrainerListVO;
import com.woniu.service.ITrainerService;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/trainer")
@Slf4j
@Tag(name = "培训师管理")
public class TrainerController {

    @Autowired
    private ITrainerService trainerService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询+搜索
     *
     * @param trainerSearchDTO
     * @return
     */
    @PostMapping("/list/search")
    public Result<PageResult<TrainerListVO>> listSearch(
            @RequestBody TrainerSearchDTO trainerSearchDTO) {
        PageResult<TrainerListVO> pageResult = trainerService.listSearch(trainerSearchDTO);
        return Result.ok(pageResult);
    }

    /**
     * 新增
     *
     * @param trainer
     * @return
     */
    @PostMapping
    public Result save(@Valid @RequestBody Trainer trainer) {

        boolean flag = trainerService.save(trainer);

        if (flag) {
            // 将图片存到redis的集合中redis_db_upload_image
            String filename = trainer.getAvatar().substring(trainer.getAvatar().indexOf(".com") + 5, trainer.getAvatar().indexOf("?"));
            redisTemplate.opsForSet().add("redis_db_upload_image", filename);
        }

        return Result.commonByFlag(flag);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Integer> ids) {
        boolean flag = trainerService.removeByIds(ids);
        return Result.commonByFlag(flag);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Trainer> getById(@PathVariable Long id) {
        Trainer trainer = trainerService.getById(id);
        return Result.ok(trainer);
    }

    /**
     * 更新
     *
     * @param trainer
     * @return
     */
    @Caching(evict = {
            @CacheEvict(value = "trainer:listAll", allEntries = true)
    })
    @PutMapping
    public Result updateById(@Valid @RequestBody Trainer trainer) {
        boolean flag = trainerService.updateById(trainer);
        if (flag) {
            // 将图片存到redis的集合中redis_db_upload_image
            String filename = trainer.getAvatar().substring(trainer.getAvatar().indexOf(".com") + 5, trainer.getAvatar().indexOf("?"));
            redisTemplate.opsForSet().add("redis_db_upload_image", filename);
        }
        return Result.commonByFlag(flag);
    }

    @GetMapping("/list/all")
    public Result<List<Trainer>> listAll() {
        List<Trainer> trainerList = trainerService.listAll();
        return Result.ok(trainerList);
    }

    /**
     * 查询培训师姓名
     * @return
     */
    @GetMapping("/listTrainer")
    Result<List<Trainer>> listTrainer() {
        List<Trainer> trainerList = trainerService.listTrainerName();
        return Result.ok(trainerList);
    }

}